// Copyright 2009 Google Inc. All Rights Reserved.

package spanishgringo.logic;

import spanishgringo.data.AnalyticsServiceWrapper;
import spanishgringo.data.AuthorizationServiceAuthSubImpl;
import spanishgringo.data.AuthorizationServiceOauthImpl;
import spanishgringo.data.PMF;
import spanishgringo.data.TokenDaoJdoImpl;

import com.google.appengine.api.users.UserServiceFactory;
import com.google.gdata.client.analytics.AnalyticsService;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;


/**
 * Factory to create GoogleDataManager objects. 
 * @author api.nickm@google.com (Nick Mihailovski)
 */
public class GoogleDataManagerFactory {

  /**
   * Constructor.
   */
  private GoogleDataManagerFactory() {}

  /**
   * Builds a new GoogleDataManager class to retrieve data from the Google Analytics API
   * using authSub authorization.
   * @param applicationName The name to pass to the AnalyticsService Object
   * @param tableId the Google Analytics table id.
 * @param segment 
   * @return a GoogleDataManager object.
   */
  public static GoogleDataManager getAuthSubManager(String applicationName, String tableId, String startDate, String endDate, String segment, String segmentName) {
    return new GoogleDataManager(
        UserServiceFactory.getUserService(),
        new TokenDaoJdoImpl(
            PMF.getInstance()),
        new AnalyticsServiceWrapper(
            new AnalyticsService(applicationName),
            new AuthorizationServiceAuthSubImpl(
                GoogleDataManager.GOOGLE_DATA_SCOPE),
            tableId, startDate, endDate, segment, segmentName));
  }
  
  /**
   * Builds a new GoogleDataManager class to retrieve data from the Google Analytics API
   * using oAuth authorization.
   * @param applicationName The name to pass to the AnalyticsService Object
   * @param tableId the Google Analytics table id.
   * @return a GoogleDataManager object.
   */
  public static GoogleDataManager getOauthManager(String applicationName, String tableId, String startDate, String endDate) {
    return new GoogleDataManager(
        UserServiceFactory.getUserService(),
        new TokenDaoJdoImpl(
            PMF.getInstance()),
        new AnalyticsServiceWrapper(
            new AnalyticsService(applicationName),
            new AuthorizationServiceOauthImpl(
                GoogleDataManager.GOOGLE_DATA_SCOPE,
                new GoogleOAuthParameters()),
            tableId, startDate, endDate));
  }
}
